package cn.edu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 注册用户
 */
public class JdbcDemo04 {
    static String url = "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
    static String user = "root";
    static String password = "root";

    public static void main(String[] args) {
        try {
            Connection conn = DriverManager.getConnection(url,user,password);
            System.out.println(conn);
            System.out.println("与数据库建立好连接了~~");

            //接收用户输入的内容
            Scanner sc = new Scanner(System.in);
            System.out.println("开始注册了~~~");
            System.out.println("请输入用户名：");
            String username = sc.nextLine();
            System.out.println("请输入密码：");
            String password = sc.nextLine();
            System.out.println("请输入昵称：");
            String nickname = sc.nextLine();
            System.out.println("请输入年龄：");
            int age = sc.nextInt();

            Statement st = conn.createStatement();
//            String sql = "insert into mydb01(username,password,nickname,age) " +
//                    "values ('zhangsan','123456','张三',18)";
            String sql = "insert into mydb01(username,password,nickname,age) " +
                    "values ('" + username + "','" + password+ "','" + nickname + "'," +age + ")";
            int rows = st.executeUpdate(sql);
            if (rows > 0){
                System.out.println("受影响的行：" + rows);
                System.out.println("注册成功了~~");
            }else {
                System.out.println("注册失败了~~");
            }
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
